package com.ruoyi.rms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.rms.enums.SeatStatus;
import com.ruoyi.rms.utils.IdUtils;
import com.ruoyi.rms.utils.RetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.SeatMessageMapper;
import com.ruoyi.rms.domain.SeatMessage;
import com.ruoyi.rms.service.ISeatMessageService;

/**
 * 餐桌信息Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-07
 */
@Service
@Slf4j
public class SeatMessageServiceImpl implements ISeatMessageService 
{
    @Autowired
    private SeatMessageMapper seatMessageMapper;

    /**
     * 查询餐桌信息
     * 
     * @param seatId 餐桌信息主键
     * @return 餐桌信息
     */
    @Override
    public SeatMessage selectSeatMessageBySeatId(Long seatId)
    {
        return seatMessageMapper.selectSeatMessageBySeatId(seatId);
    }

    /**
     * 查询餐桌信息列表
     * 
     * @param seatMessage 餐桌信息
     * @return 餐桌信息
     */
    @Override
    public List<SeatMessage> selectSeatMessageList(SeatMessage seatMessage)
    {
        return seatMessageMapper.selectSeatMessageList(seatMessage);
    }

    /**
     * 查询餐桌信息列表
     *
     * @param seatMessage 餐桌信息
     * @return 餐桌信息集合
     */
    @Override
    public List<SeatMessage> selectOpenSeatMessageList(SeatMessage seatMessage) {
        return seatMessageMapper.selectOpenSeatMessageList(seatMessage);
    }

    /**
     * 新增餐桌信息
     * 
     * @param seatMessage 餐桌信息
     * @return 结果
     */
    @Override
    public int insertSeatMessage(SeatMessage seatMessage)
    {
        String seatNo = seatMessage.getSeatNo();
        if (seatNo == null) {
            log.error("餐桌编号不能为空");
            return RetUtils.ERROR;
        }

        String status = seatMessage.getStatus();
        if (status == null) {
            log.error("未选择状态");
            return RetUtils.ERROR;
        }

        // 如果未开放则不考虑去重
        if (!status.equals(SeatStatus.NOT_USED.getStatus())) {
            // 餐桌编号去重（与已开放的去重）
            List<SeatMessage> seatMessageList = seatMessageMapper.selectSeatMessageListBySeatNo(seatNo);
            if (!seatMessageList.isEmpty()) {
                log.error("添加的餐桌编号与已开放的餐桌编号重复，添加失败");
                return RetUtils.ERROR;
            }
        }

        seatMessage.setSeatId(IdUtils.createId());
        seatMessage.setCreateTime(DateUtils.getNowDate());
        seatMessage.setCreateBy(SecurityUtils.getUsername());
        return seatMessageMapper.insertSeatMessage(seatMessage);
    }

    /**
     * 修改餐桌信息
     * 
     * @param seatMessage 餐桌信息
     * @return 结果
     */
    @Override
    public int updateSeatMessage(SeatMessage seatMessage)
    {
        String seatNo = seatMessage.getSeatNo();
        if (seatNo == null) {
            log.error("餐桌编号不能为空");
            return RetUtils.ERROR;
        }

        String status = seatMessage.getStatus();
        if (status == null) {
            log.error("未选择状态");
            return RetUtils.ERROR;
        }

        // 如果未开放则不考虑去重
        if (!status.equals(SeatStatus.NOT_USED.getStatus())) {
            // 判断修改后的餐桌编号是否与其他已开放的餐桌编号重复
            Long seatId = seatMessage.getSeatId();
            List<SeatMessage> seatMessageList = seatMessageMapper.selectSeatMessageListByIaAndSeatNo(seatId, seatNo);
            if (!seatMessageList.isEmpty()) {
                log.error("修改后的餐桌编号与已开放的餐桌编号重复，修改失败");
                return RetUtils.ERROR;
            }
        }

        seatMessage.setUpdateTime(DateUtils.getNowDate());
        seatMessage.setUpdateBy(SecurityUtils.getUsername());
        return seatMessageMapper.updateSeatMessage(seatMessage);
    }

    /**
     * 修改餐桌信息
     *
     * @param seatMessage 餐桌信息
     * @return 结果
     */
    @Override
    public int updateSeatMessageByWx(SeatMessage seatMessage)
    {
        seatMessage.setUpdateTime(DateUtils.getNowDate());
        return seatMessageMapper.updateSeatMessage(seatMessage);
    }

    /**
     * 批量删除餐桌信息
     * 
     * @param seatIds 需要删除的餐桌信息主键
     * @return 结果
     */
    @Override
    public int deleteSeatMessageBySeatIds(Long[] seatIds)
    {
        return seatMessageMapper.deleteSeatMessageBySeatIds(seatIds);
    }

    /**
     * 删除餐桌信息信息
     * 
     * @param seatId 餐桌信息主键
     * @return 结果
     */
    @Override
    public int deleteSeatMessageBySeatId(Long seatId)
    {
        return seatMessageMapper.deleteSeatMessageBySeatId(seatId);
    }
}
